package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Admin;
import dao.AdminDAO;



public class DologinServlet extends HttpServlet {


	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		PrintWriter out = response.getWriter();
		
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		Admin admin=new Admin();
		
		AdminDAO admindao=new AdminDAO();
		

		admin=admindao.checkAdmin(username, password);
		
		if(admin != null)
		{	
			session.setAttribute("username",username );
			session.setAttribute("userid", admin.getId());
			session.setAttribute("userkind", admin.getKind());
			
			out.print("<script>alert('登录成功！');location.href='admin/index.jsp';</script>");			
		}
		else
		{
			out.print("<script>alert('登录失败！');history.go(-1);</script>");
		}
		
		
		
	}

}
